package com.ruoyi.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.vo.AjaxResult;
import com.ruoyi.common.core.vo.LoginUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;

public class BaseController {
	
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected TableDataInfo getDataTable(List<?> list) {
		TableDataInfo rspData = new TableDataInfo();
		rspData.setCode(HttpStatus.SUCCESS);
		rspData.setMsg("查询成功");
		rspData.setRows(list);
		rspData.setTotal(new PageInfo(list).getTotal());
		return rspData;
	}
	
	/**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageUtils.startPage();
    }
	
    public LoginUser getLoginUser() {
    	return SecurityUtils.getLoginUser();
    }
    
    public Long getUserId() {
    	return getLoginUser().getUserId();
    }
    
    public AjaxResult error(String message) {
    	return AjaxResult.error(message);
    }
    
    public String getUsername() {
        return getLoginUser().getUsername();
    }
    
    protected AjaxResult toAjax(int rows) {
    	return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }
    
    public AjaxResult warn(String message) {
        return AjaxResult.warn(message);
    }
}
